package cn.iocoder.yudao.module.base.controller.admin.productgroup;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.base.controller.admin.productgroup.vo.*;
import cn.iocoder.yudao.module.base.dal.dataobject.productgroup.ProductGroupDO;
import cn.iocoder.yudao.module.base.service.productgroup.ProductGroupService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 产品群组")
@RestController
@RequestMapping("/base/product-group")
@Validated
public class ProductGroupController {

    @Resource
    private ProductGroupService productGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建产品群组")
    @PreAuthorize("@ss.hasPermission('base:product-group:create')")
    public CommonResult<Long> createProductGroup(@Valid @RequestBody ProductGroupSaveReqVO createReqVO) {
        return success(productGroupService.createProductGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品群组")
    @PreAuthorize("@ss.hasPermission('base:product-group:update')")
    public CommonResult<Boolean> updateProductGroup(@Valid @RequestBody ProductGroupSaveReqVO updateReqVO) {
        productGroupService.updateProductGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品群组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:product-group:delete')")
    public CommonResult<Boolean> deleteProductGroup(@RequestParam("id") Long id) {
        productGroupService.deleteProductGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品群组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:product-group:query')")
    public CommonResult<ProductGroupRespVO> getProductGroup(@RequestParam("id") Long id) {
        ProductGroupDO productGroup = productGroupService.getProductGroup(id);
        return success(BeanUtils.toBean(productGroup, ProductGroupRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品群组列表")
    @PreAuthorize("@ss.hasPermission('base:product-group:query')")
    public CommonResult<List<ProductGroupRespVO>> getProductGroupList(@Valid ProductGroupListReqVO listReqVO) {
        List<ProductGroupDO> list = productGroupService.getProductGroupList(listReqVO);
        return success(BeanUtils.toBean(list, ProductGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品群组 Excel")
    @PreAuthorize("@ss.hasPermission('base:product-group:export')")
    @OperateLog(type = EXPORT)
    public void exportProductGroupExcel(@Valid ProductGroupListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<ProductGroupDO> list = productGroupService.getProductGroupList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "产品群组.xls", "数据", ProductGroupRespVO.class,
                        BeanUtils.toBean(list, ProductGroupRespVO.class));
    }

}